package hhm.spring.security;


import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LogoutSuccHandler implements LogoutSuccessHandler {

	LogoutRequestHandler requestHandler = null;
	private SqlSession sqlSessionMySql;

	private SqlSessionFactory sqlSessionFactoryMySql;

	@Override
	public void onLogoutSuccess(HttpServletRequest request,
			HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {
		SecurityContextHolder.getContext().setAuthentication(authentication);
		requestHandler.handle(request, response, authentication);
		if ((authentication == null) || (authentication.getName() == null)
				|| (authentication.getName().equals(""))) {
			return;
		}



		SecurityContextHolder.clearContext();
	}

	public void setRequestHandler(LogoutRequestHandler requestHandler) {
		this.requestHandler = requestHandler;
	}

	public SqlSession getSqlSessionMySql() {
		return sqlSessionMySql;
	}

	public void setSqlSessionMySql(SqlSession sqlSessionMySql) {
		this.sqlSessionMySql = sqlSessionMySql;
	}

	public SqlSessionFactory getSqlSessionFactoryMySql() {
		return sqlSessionFactoryMySql;
	}

	public void setSqlSessionFactoryMySql(
			SqlSessionFactory sqlSessionFactoryMySql) {
		this.sqlSessionFactoryMySql = sqlSessionFactoryMySql;
	}
}
